home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 101-125 / disk_107 / csh / rawconsole.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  250 lines

  1. /*
  2.  * RawConsole.c
  3.  *
  4.  * Shell 2.07M    17-Jun-87
  5.  * console handling, command line editing support for Shell
  6.  * using new console packets from 1.2.
  7.  * Written by Steve Drew. (c) 14-Oct-86.
  8.  * 16-Dec-86 Slight mods to rawgets() for Disktrashing.
  9.  *
  10.  */
  11.  
  12. #if RAW_CONSOLE
  13. extern int aux; /* for use with aux: */
  14.  
  15. #include "shell.h"
  16.  
  17. void
  18. setraw(onoff)
  19. {
  20.     if (onoff) set_raw();
  21.     else set_con();
  22. }
  23.  
  24. char *
  25. rawgets(line,prompt)
  26. char *line, *prompt;
  27. {
  28.     char *get_var();
  29.     char *gets();
  30.     register int n, pl;
  31.     register int max, i;
  32.     unsigned char c1,c2,c3;
  33.     char fkeys[5];
  34.     char *s, *tyahdptr;
  35.     char *ps;
  36.     int fkey, savn;
  37.     int insert = 1;
  38.     char rep[20];
  39.     char typeahd[256];
  40.     static int width;
  41.     int recall = -1;
  42.     struct HIST *hist;
  43.  
  44.     if (aux) {
  45.     printf("%s",prompt);
  46.     fflush(stdout);
  47.     }
  48.     if (!IsInteractive(Input()) || aux ) return(gets(line));
  49.     if (WaitForChar((long)Input(), 100L) ||   /* don't switch to 1L ...*/
  50.        stdin->_bp < stdin->_bend) {        /* else causes read err's*/
  51.     /*    printf("%s",prompt); */
  52.     gets(line);
  53.     return(line);
  54.     }
  55.     setraw(1);
  56.     printf("\015%s\2336n",prompt);
  57.     savn = pl = n = 0;
  58.     tyahdptr = typeahd;
  59.     while((typeahd[n]=getchar()) != 'R') {        
  60.         if ((unsigned char)typeahd[n] == 155) savn = n;
  61.         n++;
  62.     }
  63.         
  64.     /* typeahd now contains possible type a head chars
  65.        followed by <CSI> cursor position report.
  66.      */
  67.     typeahd[savn]  = '\0';
  68.     if (typeahd[n-2] != ';') {
  69.         pl = (typeahd[n-2] - 48) * 10;
  70.     }
  71.     pl += typeahd[n-1] - 49;
  72.     ps = line + pl;
  73.     line[max = i = pl] = '\0';
  74.     
  75.     if (!width) width = 77;
  76.     if (s = get_var (LEVEL_SET, "_insert"))
  77.     insert = atoi(s) ? 1 : 0;
  78.     
  79.     while( (c1 = *tyahdptr) != '\0' || (c1 = getchar()) != 255) {
  80.         if (*tyahdptr) ++tyahdptr;
  81.     switch(c1) {
  82.         case 155:
  83.          c2 = getchar();
  84.          switch(c2) {
  85.              case 'A':            /* up arrow   */
  86.             n = ++recall;
  87.              case 'B':            /* down arrow */
  88.             line[pl] = '\0';
  89.             if (recall >= 0 || c2 == 'A') {
  90.                 if (c2 == 'B') n = --recall;
  91.                 if (recall >= 0) {
  92.                 for(hist = H_head; hist && n--;
  93.                     hist = hist->next);
  94.                 if (hist) strcpy(&line[pl],hist->line);
  95.                 else recall = H_len;
  96.                 }
  97.             }
  98.             if (i != pl)
  99.                 printf("\233%dD",i);
  100.             printf("\015\233J%s%s",prompt,ps);
  101.             i = max = strlen(ps) + pl;
  102.             break;
  103.              case 'C':            /* right arrow*/
  104.             if (i < max) {
  105.                 i++;
  106.                 printf("\233C");
  107.             }
  108.             break;
  109.              case 'D':            /* left arrow */
  110.             if (i > pl) {
  111.                 i--;
  112.                 printf("\233D");
  113.             }
  114.             break;
  115.             case 'T':            /* shift up   */
  116.             case 'S':            /* shift down */
  117.             break;
  118.             case ' ':            /* shift -> <-*/
  119.             c3 = getchar();
  120.             break;
  121.             default:
  122.             c3 = getchar();
  123.             if (c3 == '~') {
  124.                 fkey = c2;
  125.                 fkeys[0] = 'f';
  126.                 if (c2 == 63) {
  127.                 strcpy(ps,"help");
  128.                 goto done;
  129.                 }
  130.             }
  131.             else if (getchar() != '~') { /* window was resized */
  132.                 while(getchar() != '|');
  133.                 printf("\2330 q"); /* get window bounds */
  134.                 n = 0;
  135.                 while((rep[n] = getchar()) != 'r' && n++ < 20);
  136.                 width = (rep[n-3] - 48) * 10 + rep[n-2] - 48;
  137.                 rep[n-1] = '\0';
  138.                 set_var (LEVEL_SET, "_width", &rep[n-3]);
  139.                 break;
  140.             }
  141.             else {
  142.                 fkey = c3;
  143.                 fkeys[0] = 'F';
  144.             }
  145.             sprintf(fkeys+1,"%d",fkey - 47);
  146.             if (!(s = get_var(LEVEL_SET, fkeys))) break;
  147.                 tyahdptr = strcpy(typeahd,s);
  148.             break;
  149.             }
  150.         break;
  151.         case 8:
  152.         if (i > pl) {
  153.             i--;
  154.             printf("\010");
  155.         }
  156.         else break;
  157.         case 127:
  158.         if (i < max) {
  159.             int j,t,l = 0;
  160.             movmem(&line[i+1],&line[i],max-i);
  161.             --max;
  162.             printf("\233P");
  163.             j = width - i % width - 1;     /* amount to end     */
  164.             t = max/width - i/width;     /* no of lines          */
  165.             for(n = 0; n < t; n++) {
  166.             l += j;             /* no. of char moved */
  167.             if (j) printf("\233%dC",j); /* goto eol          */
  168.             printf("%c\233P",line[width*(i/width+n+1)-1]);
  169.             j = width-1;
  170.             }
  171.             if (t)
  172.             printf("\233%dD",l+t);   /* get back */
  173.         }
  174.         break;
  175.         case 18:
  176.         n = i/width;
  177.         if (n) printf("\233%dF",n);
  178.         printf("\015\233J%s%s",prompt,ps);
  179.         i = max;
  180.         break;
  181.         case 27:
  182.         break;
  183.         case 1:
  184.         insert ^= 1;
  185.         break;
  186.         case 21:
  187.         case 24:
  188.         case 26:
  189.         if (i > pl)
  190.             printf("\233%dD",i-pl);
  191.         i = pl;
  192.         if (c1 == 26) break;
  193.         printf("\233J");
  194.         max = i;
  195.         line[i] = '\0';
  196.         break;
  197.         case 11:        /* ^K */
  198.         printf("\233J");
  199.         max = i;
  200.         line[i] = '\0';
  201.         break;
  202.         case 28:        /* ^\ */
  203.         setraw(0);
  204.         return(NULL);
  205.         case 5:
  206.         printf("\233%dC",max - i);
  207.         i = max;
  208.         break;
  209.         case 10:
  210.         case 13:
  211.         line[max] = '\0';
  212. done:        printf("\233%dC\n",max - i);
  213.  
  214.         setraw(0);
  215.         strcpy(line, ps);
  216.         return(line);
  217.         default:
  218.             c1 &= 0x7f;
  219.         if (c1 == 9) c1 = 32;
  220.         if (c1 > 31 & i < 256) {
  221.             if (i < max && insert) {
  222.             int j,t,l = 0;
  223.             movmem(&line[i], &line[i+1], max - i);
  224.             printf("\233@%c",c1);
  225.             t = max/width - i/width;
  226.             j = width - i % width - 1;
  227.             for(n = 0; n < t; n++) {
  228.                 l += j;
  229.                 if (j) printf("\233%dC",j);
  230.                 printf("\233@%c",line[width*(i/width+n+1)]);
  231.                 j = width-1;
  232.             }
  233.             if (t) printf("\233%dD",l + t);
  234.             ++max;
  235.             }
  236.             else {
  237.             if(i == pl && max == i) printf("\015%s%s",prompt,ps);
  238.             putchar(c1);
  239.             }
  240.             line[i++] = c1;
  241.             if (max < i) max = i;
  242.             line[max] = '\0';
  243.         }
  244.     }
  245.     }
  246.     setraw(0);
  247.     return(NULL);
  248. }
  249. #endif
  250.